package uf.tax.laws.spider;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Properties;

/**
 * 工具单元
 * 
 * @author sunny
 *
 */
public class Utils {
	public static void strToFile(String content, String filePath, String encoding) throws IOException {
		FileOutputStream foStream = new FileOutputStream(filePath);
		byte[] buffer = content.getBytes(encoding);
		foStream.write(buffer);
		foStream.flush();
		foStream.close();
	}

	public static String fileToStr(String filePath, String encoding) throws IOException {
		FileInputStream foStream = new FileInputStream(filePath);
		byte[] buffer = new byte[foStream.available()];
		foStream.read(buffer);
		foStream.close();
		return new String(buffer, encoding);
	}

	public static Properties getConfig(String path) {
		Properties p = new Properties();
		FileInputStream fis;
		try {
			fis = new FileInputStream(path);
			p.load(fis);
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

	public static void saveProperties(Properties p, String path, String comment) {
		FileOutputStream out;
		try {
			out = new FileOutputStream(path);
			p.store(out, comment);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void print(String msg) {
		System.out.println(msg);
	}

	public static String encode(String str, String encoding) {
		try {
			return URLEncoder.encode(str, encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String decode(String str, String encoding) {
		try {
			return URLEncoder.encode(str, encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
}
